package com.example.demo.util;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

@Component
public class RedisStringUtil {

	@Autowired
	private ValueOperations<String, Object> valueOperations;
	
	public void set(String key, String value) {
		valueOperations.set(key, value);
	}
	
	public String get(String key) {
		return (String) valueOperations.get(key);
	}
	
	public String getset(String key, String value) {
		return (String) valueOperations.getAndSet(key, value);
	}
	
	public void setex(String key, long seconds, String value) {
		valueOperations.set(key, value, seconds, TimeUnit.SECONDS);
	}
	
	public boolean setnx(String key, String value) {
		return valueOperations.setIfAbsent(key, value);
	}
	
	public long incr(String key) {
		return valueOperations.increment(key, 1);
	}
	
	public long decr(String key) {
		return valueOperations.increment(key, -1);
	}
	
	public long incrby(String key, int increment) {
		return valueOperations.increment(key, increment);
	}
	
	public long decrby(String key, int decrement) {
		return valueOperations.increment(key, Math.abs(decrement)*-1);
	}
	
	public long strlen(String key) {
		return valueOperations.size(key);
	}
	
	public int append(String key, String value) {
		return valueOperations.append(key, value);
	}
	
	public void mset(Map<String, Object> map) {
		valueOperations.multiSet(map);
	}
	
	public List<Object> mget(String ...keys) {
		return valueOperations.multiGet(Arrays.asList(keys));
	}
	
	public boolean msetnx(Map<String, Object> map) {
		return valueOperations.multiSetIfAbsent(map);
	}
}
